#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import cgi
import wsgiref.handlers
import os
from google.appengine.api import users
from google.appengine.ext.webapp import template
from google.appengine.ext import webapp
from google.appengine.ext import db
from google.appengine.ext.webapp.util import run_wsgi_app

from general_db import GeneralDb
import time 
import datetime 

class DataInput(webapp.RequestHandler):

  def get(self):
    if users.get_current_user():
      url = users.create_logout_url(self.request.uri)
      url_linktext = users.get_current_user().nickname() + ' Logout'
    else:
      url = users.create_login_url(self.request.uri)
      url_linktext = 'Anonymous Login'
    template_values = {
      'url': url,
      'url_linktext': url_linktext,
    }
    path = os.path.join(os.path.dirname(__file__), 'data_input.html')
    self.response.out.write(template.render(path, template_values))

  def post(self):
    if users.get_current_user():
      count = users.get_current_user().nickname()
    else:
      count = 'a dog ?'
    date_str = self.request.get("date")
    date = time.strptime(date_str,"%m/%d/%Y") 
    content = self.request.get("content")
    tag = self.request.get("tag")
    generaldb = GeneralDb()
    generaldb.author = users.get_current_user()
    generaldb.content = content
    generaldb.date_input = datetime.datetime(date[0],date[1],date[2]) 
    generaldb.tag = tag
    generaldb.put()
    template_values = {
      'count': count,
      'date': date,
      'content': content,
      'tag': tag,
    }
    path = os.path.join(os.path.dirname(__file__), 'data_added.html')
    self.response.out.write(template.render(path, template_values))
    pass
